package light.head.dao.inv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvInStorage;
@Service
public class InvInStorageDAO extends BaseDAO<InvInStorage, Integer>{
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(ocs == null || ocs.size() == 0) return null;
			
		StringBuffer hql = new StringBuffer("FROM InvInStorage iis WHERE iis.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String dn =map.get(Parm.DN);
			String cn = map.get(Parm.CN);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			String bname = map.get(Parm.BNAME);
			
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND iis.deleted = ?");
			}
			if (isNotNull(dn)) {
				hql.append(" AND iis.dn LIKE ?");
				list.add("%" + dn + "%");
			}
			if(isNotNull(bname)){
				hql.append(" AND iis.bc IN (SELECT ib.code FROM InvBusiness ib WHERE ib.name LIKE ?)");
				list.add("%" + bname + "%");
			}
			if (isNotNull(cn)) {
				hql.append(" AND iis.cu IN (SELECT ui.code FROM UserInfo ui WHERE ui.realName LIKE ?)");
				list.add("%" + cn + "%");
			}if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND iis.ct >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND iis.ct <= ?");
			}
		}
		hql.append(" ORDER BY iis.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	public List<InvInStorage> findCode(List<Integer> ocs,String bname,String time1,String time2) {
		StringBuffer sql = new StringBuffer("from InvInStorage iis WHERE iis.oc IN (");
		List<Object> parm = new ArrayList<Object>();
		for (Integer i : ocs) {
			sql.append("?,");
			parm.add(i);
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		if(isNotNull(time1)){
			sql.append(" AND iis.ct > ?");
			parm.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
		}
		if(isNotNull(time2)){
			sql.append(" AND iis.ct < ?");
			parm.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
		}
		if(isNotNull(bname)){
			sql.append(" AND iis.bc IN (SELECT ib.code FROM InvBusiness ib WHERE ib.name LIKE ?)");
			parm.add("%" + bname + "%");
		}
		return find(sql.toString(), parm.toArray());
	}
}
